package com.example.order.activity;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.JsonReader;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.order.R;
import com.example.order.pojo.Customer;
import com.example.order.pojo.Desk;
import com.example.order.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    String deskId;
    Integer order_id;
    Customer customer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //getActionBar().setTitle("用户主页");
        //TextView text=findViewById(R.id.text);
        final TextView text_desk = findViewById(R.id.text_desk);
        final ListView list=findViewById(R.id.list_desk);
        //final String[] deskId = new String[1];
        //监听两个单选框
        RadioButton button1=findViewById(R.id.button_1);//预定
        RadioButton button2=findViewById(R.id.button_2);//立即就餐
        Button button3=findViewById(R.id.button_3);//去点菜
        Button button4=findViewById(R.id.button_4);//历史订单
        Button button5=findViewById(R.id.button_5);//查看个人信息
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);

        final List<String> lists=new ArrayList<>();
        customer=(Customer)getIntent().getSerializableExtra("customer");
        assert customer != null;
        Log.i("Main2Activity",customer.toString());
        Utils.showToast(this,customer.toString(),false);
        Utils.sendHttpRequest("desk/findEmpty", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("Main2Activity", Arrays.toString(e.getStackTrace()));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Utils.showToast(Main2Activity.this,"获取桌子状态失败",true);
                    }
                });

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String strings = response.body().string();
                Log.i("Main2Activity", strings);
                Gson gson=new Gson();
                List<Desk> desks=gson.fromJson(strings,new TypeToken<List<Desk>>(){}.getType());
                //只保留id

                for (int i = 0; i < desks.size(); i++) {
                    lists.add("桌子编号:"+String.valueOf(desks.get(i).getId())+" 状态:空");
                    //lists.add(String.valueOf(desks.get(i).getId()));
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayAdapter<String> adapter=new ArrayAdapter<>(Main2Activity.this,R.layout.support_simple_spinner_dropdown_item, lists);
                        list.setAdapter(adapter);
                    }
                });


            }
        });
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //桌号
                deskId =lists.get(position).substring(5,7);
                Utils.showToast(Main2Activity.this,"您选择的桌号是:"+ deskId,false);
                text_desk.setText("您选择的桌号是:"+ deskId);
            }
        });

    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.button_1:
                //预定
                Utils.showToast(this,"您点击了预定",false);
                break;
            case R.id.button_2:
                //立即就餐
                Utils.showToast(this,"您点击了立即就餐",false);
                break;
            case R.id.button_3:
                //Utils.showToast(this,"您点击了去点菜",false);
                //startActivity(new Intent(Main2Activity.this,TestActivity.class));
                //需要传的参数:customer_id desk_id


                if(Utils.isNotNullString(deskId))
                {
                    HashMap<String,String> map=new HashMap<>();
                    Customer customer =(Customer) getIntent().getSerializableExtra("customer");
                    map.put("customer_id",String.valueOf(customer.getId()));
                    map.put("desk_id",deskId);
                    Utils.showToast(Main2Activity.this,map.toString(),true);
                    Utils.sendPostHttpRequest("order/start", map, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            final String string = response.body().string();
                            Log.e("Main2Activity",string);
                            //传回的值为order_id
                            if(Utils.isNotNullString(string))
                            {
                                order_id=Integer.valueOf(string);

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Utils.showToast(Main2Activity.this,string,false);
                                    }
                                });

                                Intent intent=new Intent(Main2Activity.this,Main3Activity.class);
                                //Customer类可序列化
                                intent.putExtra("customer",getIntent().getSerializableExtra("customer"));
                                intent.putExtra("order_id",String.valueOf(order_id));
                                //startActivity(new Intent(Main2Activity.this,Main3Activity.class));
                                //此处不能new Intent对象，不然Main3Activity获取到的值都为Null
                                startActivity(intent);
                            }

                        }
                    });
                }
                else
                {
                    Utils.showToast(Main2Activity.this,"请选择就餐的桌子",false);
                }
                break;
            case R.id.button_4:
                //查看历史订单
                Utils.showToast(Main2Activity.this,"查看历史订单",false);
                Intent intent1 = new Intent(Main2Activity.this, Main5Activity.class);
                //intent1.putExtra("phone",customer.getPhone());
                intent1.putExtra("id",customer.getId());
                startActivity(intent1);
                break;
            case R.id.button_5:
                //查看个人信息(修改口味、忌口)
                Utils.showToast(Main2Activity.this,"查看/修改个人信息",false);
                Intent intent2 = new Intent(Main2Activity.this, Main6Activity.class);
                //intent2.putExtra("phone",customer.getPhone());
                intent2.putExtra("customer",customer);
                startActivity(intent2);
                break;
            default:
                break;
        }

    }
}
